using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using T2.WebSecurity.ASPMVC.Entity.Interface;
using T2.WebSecurity.ASPMVC.Entity;
using T2.WebSecurity.Business.Entities;
using T2.WebSecurity.Business.Entities.Interfaces;
using T2.WebSecurity.ASPMVC.Entity.Search;
using T2.WebSecurity.ASPMVC.Entity.Search.Interface;
using T2.WebSecurity.Business.Entities.Search;
using T2.WebSecurity.Business.Entities.Search.Interfaces;
using T2.WebSecurity.ASPMVC.Entity.Translators;
using T2.WebSecurity.ASPMVC.Entity.Translators.Search;
using System.Web.Security;
using System.Activities;
using T2.WebSecurity.Business.Workflows;


namespace T2.WebSecurity.ASPMVC.ViewModel
{

	public class UserProfileViewModel : BaseViewModel
	{
		Business.BusinessService bs = new Business.BusinessService();
		public UserProfileViewModel(bool loadParent)
		{
		if (loadParent)
			{
			}
		}
		
		private String m_header;
		public String Header
        {
            get
            {
                return "User Profile";
            }
			
			set
            {
                m_header = value;
            }
        }
		
		private String m_title;
		public String Title
        {
            get
            {
                return "User Profile";
            }
			
			set
            {
                m_title = value;
            }
        }
		
		private IUserProfile_ui m_model;
		public IUserProfile_ui Model
		{
			get
			{
				return m_model;
			}
			set
			{
				m_model = value;
			}
		}

        
        
		
		public void LoadModel(IUserProfile_ui param)
        {
            UserProfileUITranslator trans = new UserProfileUITranslator();
            IUserProfile_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new UserProfileQuery(), wfInput);

            IUserProfile_biz retValBiz = (IUserProfile_biz)wfOutput["RetVal"];

            //List<IUserProfile_biz> tempRetVal = FilterListByLoginUser(bs.UserProfileController_Get(bizParam, true, true));
            IUserProfile_ui retVal = new UserProfile_ui();
            if (retValBiz != null)
            {
                retVal = trans.TranslateFromBusinessLayer(retValBiz);
            }

            m_model = retVal;

        }

        public Int32 Set(IUserProfile_ui param)
        {
            UserProfileUITranslator trans = new UserProfileUITranslator();
            IUserProfile_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new UserProfileNonQuery(), wfInput);

            IDictionary<string, int> r = (IDictionary<string, int>)wfOutput["RetVal"];
            //IDictionary<string,int> r = bs.UserProfileController_Set(bizParam);
            var ret = r.ToList().Select(s => new { Key = s.Key, Value = s.Value });
            Int32 retVal = ret.Where(z => z.Key == "Return_Value").Select(m => m.Value).FirstOrDefault();
			return retVal;
        }

       

        public List<IUserProfile_ui> SearchList(IUserProfileSearch_ui param)
        {
            UserProfileUISearchTranslator transSearch = new UserProfileUISearchTranslator();
            IUserProfileSearch_biz bizParam = transSearch.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", null);
            wfInput.Add("ParamSearch", bizParam);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new UserProfileQuery(), wfInput);

            IUserProfile_biz[] retValBiz = (IUserProfile_biz[])wfOutput["RetValCollection"];

            //List<IUserProfile_biz> retValBiz = FilterListByLoginUser(bs.UserProfileController_Search(bizParam, true, true));
            List<IUserProfile_ui> retVal = new List<IUserProfile_ui>();
            UserProfileUITranslator trans = new UserProfileUITranslator();
            foreach (IUserProfile_biz item in retValBiz)
            {
                IUserProfile_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }

        

        public List<IUserProfile_ui> ModelList(IUserProfile_ui param)
        {

            UserProfileUITranslator trans = new UserProfileUITranslator();
            IUserProfile_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new UserProfileQuery(), wfInput);

            IUserProfile_biz[] retValBiz = (IUserProfile_biz[])wfOutput["RetValCollection"];

            //List<IUserProfile_biz> retValBiz = FilterListByLoginUser(bs.UserProfileController_Get(bizParam, true, true));
            List<IUserProfile_ui> retVal = new List<IUserProfile_ui>();
            foreach (IUserProfile_biz item in retValBiz)
            {
                IUserProfile_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }

        
		
	}
}
